import { getPlRequest } from '@/apis/request';
import config from '../config';
import http from './http';
import type { DataHeader, BlockRankParams, RankParams, PurchasedRes } from '@/types/api.d';

// 判断是否是小程序及ios平台，如果是则使用原生请求 - 由于isThsMiniProgram的判断有问题，本次暂时不用此判断
// const req = isThsMiniProgram && window.getPlatform() === 'iphone' ? getPlRequest : http;
const req = window.getPlatform() === 'iphone' ? getPlRequest : http;
// 单系统得分信息详情
export function getStockScore(stockCode: string, system: string) {
  const url = `${config.api.FUYAO_HOST}/stock_health_diagnosis/stock_score/v1/info`;
  const paramsOption = {
    stock_code: stockCode,
    system: system,
  };
  return req({ url, data: paramsOption });
}

// 判断当前用户具有哪一种权限
export function getAuth(productNameList: string[] = ['ssgsjkzszs', 'ssgsjkzszyb']) {
  const url = `${config.api.APIGATE_HOST}/d/charge/smallcharge/l2/v1/auth`;
  return req({ url, data: { func_list: productNameList.join(',') } });
}

// 公司得分总览
export function getCompanyOverview(stockCode: string) {
  return req({
    url: `${config.api.FUYAO_HOST}/stock_health_diagnosis/stock_score/v2/overview`,
    data: {
      stock_code: stockCode,
    },
  });
}

// 公司得分历史
export function getCompanyHistory(blockType: string, blockCode?: string) {
  return req({
    url: `${config.api.FUYAO_HOST}/stock_health_diagnosis/score/v1/history`,
    data: {
      block_type: blockType,
      block_code: blockCode || '',
    },
  });
}

// 个股得分历史
export function getStockHistory(stockCode: string) {
  return req({
    url: `${config.api.FUYAO_HOST}/stock_health_diagnosis/stock_score/v1/history`,
    data: {
      stock_code: stockCode,
    },
  });
}

// 个股行情
export function getStockHq(param: DataHeader) {
  return req({
    url: `${config.api.FUYAO_HOST}/stock_health_diagnosis/stock_hq/v1/info`,
    data: param,
  });
}

// 得分分布
export function getMarketDistribution(param: DataHeader) {
  return req({
    url: `${config.api.FUYAO_HOST}/stock_health_diagnosis/score/v1/distribution`,
    method: 'POST',
    data: param,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded',
    },
  });
}

// 得分分布详情
export function getMarketDistributionDetail(param: DataHeader) {
  return req({
    url: `${config.api.FUYAO_HOST}/stock_health_diagnosis/score/v1/distribution_info`,
    method: 'POST',
    data: param,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded',
    },
  });
}

// 得分统计
export function getMarketStatistics(param: DataHeader) {
  return req({
    url: `${config.api.FUYAO_HOST}/stock_health_diagnosis/score/v1/info`,
    method: 'POST',
    data: param,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded',
    },
  });
}

// 行业/地区 得分列表
export function getIndustryOrRegionScore(blockType: string) {
  return req({
    url: `${config.api.FUYAO_HOST}/stock_health_diagnosis/score/v1/list`,
    data: {
      block_type: blockType,
    },
  });
}

// 个股行业内对比详情
export function getIndustryCompare(stockCode: string) {
  return req({
    url: `${config.api.FUYAO_HOST}/stock_health_diagnosis/compare/v1/info`,
    data: {
      stock_code: stockCode,
    },
  });
}

// 个股行业内对比得分分布
export function getCompareDistribution(stockCode: string) {
  return req({
    url: `${config.api.FUYAO_HOST}/stock_health_diagnosis/compare/v1/distribution`,
    data: {
      stock_code: stockCode,
    },
  });
}

export function getRankingList(obj: RankParams) {
  return req({
    url: `${config.api.FUYAO_HOST}/stock_health_diagnosis/score/v1/ranking_list`,
    method: 'POST',
    data: obj,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded',
    },
  });
}
export function getBlockRankingList(obj: BlockRankParams) {
  return req({
    url: `${config.api.FUYAO_HOST}/stock_health_diagnosis/score/v1/block_ranking_list`,
    method: 'POST',
    data: obj,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded',
    },
  });
}

//用户自选股得分评价
export function getUserEvaluate(stockCodes: string) {
  return req({
    url: `${config.api.FUYAO_HOST}/stock_health_diagnosis/score/v1/evaluate`,
    method: 'POST',
    data: { stock_codes: stockCodes },
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded',
    },
  });
}

//健康回测数据
export function getBackTesting() {
  return req({
    url: `${config.api.FUYAO_HOST}/stock_health_diagnosis/line/v1/back_testing`,
  });
}

// 同行业股票
export function getIndustryStockList(stockCode: string) {
  return req({
    url: `${config.api.FUYAO_HOST}/stock_health_diagnosis/stock_hq/v1/industry_stock_list`,
    data: {
      stock_code: stockCode,
      page: 1,
      page_size: 5,
    },
  });
}
//热股榜单
export function getHotStockList() {
  return req({
    url: `${config.api.EQ_HOST}/open/api/hot_list/v1/skyrocket/hour.txt`,
  });
}
// 获取已购个股列表
export function getPurchased(): Promise<PurchasedRes> {
  return req({
    url: `${config.api.FUYAO_HOST}/stock_health_diagnosis/stock_hq/v1/purchased`,
  });
}

// 搜索股票信息
export function searchStockInfo(pattern: string) {
  // 需要注意此处要固定改成客户端请求 普通请求会有跨域的问题
  return getPlRequest({
    url: `${config.api.DICT_HOST}/stocks`,
    data: {
      pattern: pattern,
      associate: 1,
      json: 1,
      field: 4,
    },
    dataType: 'GBK',
  });
}
